using System;
using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
using System.Collections.Generic;


namespace Socialite
{
	// 
	// A MonoTouch.Dialog.Element that renders a UserGroupCell
	//
	[Register ("UserGroupElement")]
	public class UserGroupElement : RootElement , IElementSizing
	{
		static NSString key = new NSString ("usergroupelement");
		public IEnumerable<User> Users;
		Action<User> _fnUserClickAction;
		float _cellHeight;
		
		public UserGroupElement (IEnumerable<User> users, Action<User> fnUserClickAction) : base (null)
		{
			_fnUserClickAction = fnUserClickAction;
			Users = users;	
		}
	
		// Gets a cell on demand, reusing cells
		public override UITableViewCell GetCell (UITableView tv)
		{
			var cell = tv.DequeueReusableCell (key) as UserGroupCell;
			if (cell == null)
			{
				// We have to allocate a cell
				var views = NSBundle.MainBundle.LoadNib ("UserGroupCell", tv, null);
				cell = Runtime.GetNSObject (views.ValueAt (0)) as UserGroupCell;
			}
			
			cell.BindDataToCell (Users, _fnUserClickAction );
	
			return cell;
		}
	
		public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
		{
			//var profile = new UserDetailsScreen (User);
			//dvc.ActivateController (profile);
			//dvc.NavigationController.PushViewController (profile, true);
		}
	
		public override bool Matches (string text)
		{
			//return (User.BasicInfo.FirstName.IndexOf (text, StringComparison.CurrentCultureIgnoreCase) != -1) || 
			//	(User.BasicInfo.LastName.IndexOf (text, StringComparison.InvariantCultureIgnoreCase) != -1);
			return true;
		}
	
		public float GetHeight (UITableView tableView, NSIndexPath indexPath)
		{
			if (_cellHeight == 0) {
				var views = NSBundle.MainBundle.LoadNib ("UserGroupCell", tableView, null);
				UserGroupCell cell = Runtime.GetNSObject (views.ValueAt (0)) as UserGroupCell;
					
				_cellHeight = cell.Bounds.Height;
			}
			
			return _cellHeight;
		}
	}
}

